/**
 *  Copyright � 2010 Digia Plc
 *  Copyright � 2010 Nokia Corporation
 *
 *  All rights reserved.
 *
 *  Nokia and Nokia Connecting People are registered trademarks of
 *  Nokia Corporation.
 *  Java and all Java-based marks are trademarks or registered
 *  trademarks of
 *  Sun Microsystems, Inc. Other product and company names
 *  mentioned herein may be
 *  trademarks or trade names of their respective owners.
 *
 *
 *  Subject to the conditions below, you may, without charge:
 *
 *  �  Use, copy, modify and/or merge copies of this software and
 *     associated documentation files (the "Software")
 *
 *  �  Publish, distribute, sub-licence and/or sell new software
 *     derived from or incorporating the Software.
 *
 *
 *  This file, unmodified, shall be included with all copies or
 *  substantial portions
 *  of the Software that are distributed in source code form.
 *
 *  The Software cannot constitute the primary value of any new
 *  software derived
 *  from or incorporating the Software.
 *
 *  Any person dealing with the Software shall not misrepresent
 *  the source of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
 *  KIND, EXPRESS OR IMPLIED,
 *  INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *  MERCHANTABILITY, FITNESS FOR A
 *  PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT
 *  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *  WHETHER IN AN ACTION
 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 *  CONNECTION WITH THE
 *  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
import Qt 4.7
import "javascript/sql.js" as HCS
import "../colibri"
import "styles"

Item {
    id: screen

    property int colSpacing : 15
    property string room : "room1"
    property bool isVisible : false
    property CLStyle style : StyleHomeControlSystem{}

    property int textSize : 28
    property color textColor: style.textColor
    property bool mute: false
    property int light : 0
    property int temp : 0
    property int volume : 0
    property string name : ""
    property bool onStartUp : true

    signal closeWindow()


    /**
     * Updates room information.
     */
    function updateInfo() {

        HCS.getTheRoom(screen.room);
        screen.name = HCS.theRoom.name;

        meter.opacity = (HCS.theRoom.temperature-adjustTemperature.minimum)/(adjustTemperature.maximum-adjustTemperature.minimum)
        meter.text = HCS.theRoom.temperature+"<html>&deg;</html>C";
        meter1.text = HCS.theRoom.temperature+"<html>&deg;</html>C";
        if (HCS.theRoom.temperature == adjustTemperature.minimum)meter.value = false
        else meter.value = true
        adjustTemperature.setValue(HCS.theRoom.temperature);

        lamp.opacity = (HCS.theRoom.light-adjustLight.minimum)/(adjustLight.maximum-adjustLight.minimum)
        if (HCS.theRoom.light == adjustLight.minimum)lamp.light = false
        else lamp.light = true
        adjustLight.setValue(HCS.theRoom.light);

        screen.mute = HCS.theRoom.mute
        if (!screen.mute && HCS.theRoom.volume != adjustVolume.minimum){
           screen.mute = false
           mute.opacity = 0
           speaker.opacity = 1
        } else {
           screen.mute = true
           mute.opacity = 1
           speaker.opacity = 0
        }

        onStartUp = true;
        adjustVolume.setValue(HCS.theRoom.volume);
        histogramValues(volume);

    }

    /**
     * Sets values to small histogram which is over loudspeaker.
     * @param value Current volume in speaker
     */
    function histogramValues(value){
        var array = new Array(3)
        if ( adjustVolume.minimum <= value && value < adjustVolume.maximum/3){
            array[0]=new Array(2*value/(adjustVolume.maximum/3),"1")
            array[1]=new Array(3*value/(adjustVolume.maximum/3),"1")
            array[2]=new Array(4*value/(adjustVolume.maximum/3),"1")
        }
        if (adjustVolume.maximum/3 <= value && value < adjustVolume.maximum/1.5){
            array[0]=new Array(3*value/(adjustVolume.maximum/1.5),"1")
            array[1]=new Array(5*value/(adjustVolume.maximum/1.5),"1")
            array[2]=new Array(7*value/(adjustVolume.maximum/1.5),"1")
        }
        if (adjustVolume.maximum/1.5 <= value && value <= adjustVolume.maximum){
            array[0]=new Array(4*value/(adjustVolume.maximum),"1")
            array[1]=new Array(7*value/(adjustVolume.maximum),"1")
            array[2]=new Array(10*value/(adjustVolume.maximum),"1")
        }
        histogram.setValues(array);
    }

    Component.onCompleted: {
        updateInfo();
        textSize = screen.height * 0.075
    }

    onHeightChanged: {
        textSize = screen.height * 0.075
    }

    onRoomChanged: {
        updateInfo();
    }

    width: 600//parent.width
    height: 480//parent.height


    Item {
        id: roomAdjustWidget
        anchors.horizontalCenter:parent.horizontalCenter
        anchors.verticalCenter: parent.verticalCenter
        width: parent.width
        height: parent.height


        Rectangle{
            width:roomAdjustWidget.width * (400/420)
            height:roomAdjustWidget.height * (255/275)
            color: style.selectionColor
            anchors.centerIn:parent
            radius:7

        }


        Image {
            id:bgPic
            source: "images/control_bg_trans_420x275_png.png";
            width:roomAdjustWidget.width
            height:roomAdjustWidget.height
            opacity:0.7
        }


        MouseArea {
            anchors.fill: parent
        }


        RoomExitButton {
            windowWidth: screen.width
            windowHeight: screen.height
            style: screen.style
            onClicked: screen.closeWindow();
        }


        Column{
            spacing: colSpacing
            anchors.verticalCenter: parent.verticalCenter
            anchors.horizontalCenter: parent.horizontalCenter
            Text {
                id: roomName
                text: screen.name
                anchors.horizontalCenter:parent.horizontalCenter
                font.bold: true
                color: screen.textColor
                font.pixelSize: textSize
            }

            Row {
                spacing: colSpacing

                Item{
                    height:roomAdjustWidget.height*0.20
                    width: height

                    Text{
                        id:meter1
                        font.pixelSize: textSize
                        color: screen.style.textColor
                        smooth:true
                        y:roomAdjustWidget.height*0.03

                    }

                    Text{
                        id:meter

                        property bool value : false

                        font.pixelSize: textSize
                        color: screen.style.textColor
                        smooth:true
                        y:roomAdjustWidget.height*0.03

                    }

                    MouseArea {
                        id: mouseArea2
                        anchors.fill: parent
                        onClicked: {
                            meter.value = !meter.value
                            if(meter.value){
                                temp=adjustTemperature.maximum
                                meter.opacity = 1
                                adjustTemperature.setValue(adjustTemperature.maximum)
                            } else {
                                temp=adjustTemperature.minimum
                                meter.opacity = 0
                                adjustTemperature.setValue(adjustTemperature.minimum)
                            }
                            meter.text = temp+"<html>&deg;</html>C";
                            meter1.text = temp+"<html>&deg;</html>C";
                        }
                    }
                }
                CLSlider{
                    id:adjustTemperature
                    style: screen.style
                    height:roomAdjustWidget.height*0.15
                    width:roomAdjustWidget.width*0.6
                    minimum:18
                    maximum:30
                    borderWidth: 3
                    onValueChanged: {
                        temp = adjustTemperature.value
                        if(temp == adjustTemperature.minimum)meter.value = false
                        else meter.value = true
                        meter.opacity = (temp-adjustTemperature.minimum)/(adjustTemperature.maximum-adjustTemperature.minimum)
                        meter.text = temp+"<html>&deg;</html>C";
                        meter1.text = temp+"<html>&deg;</html>C";
                    }

                }
            }

            Row {
                spacing: colSpacing

               Item{
                    height:roomAdjustWidget.height*0.20
                    width: height

                    Image {
                        anchors.fill:parent
                        smooth:true
                        source:"images/lamp_off_100x100.png"

                    }

                    Rectangle{
                        id:lamp

                        property bool light : false

                        color:"transparent"
                        anchors.fill:parent
                        Image{
                            anchors.fill:parent
                            smooth:true
                            source:"images/glow_200x200.png"
                        }
                        Image{
                            anchors.fill:parent
                            smooth:true
                            source:"images/lamp_on_100x100.png"
                        }
                    }

                    MouseArea {
                        id: mouseArea
                        anchors.fill: parent

                        onClicked: {
                            if(lamp.light){
                                light=adjustLight.minimum
                                adjustLight.setValue(adjustLight.minimum)
                                lamp.opacity = 0
                                lamp.light = false
                            } else {
                                light=adjustLight.maximum
                                lamp.opacity = 1
                                adjustLight.setValue(adjustLight.maximum)
                                lamp.light = true
                            }

                        }
                    }
                }
                CLSlider{
                    id:adjustLight
                    style:screen.style
                    minimum:0
                    maximum:100
                    borderWidth: 3
                    height:roomAdjustWidget.height*0.15
                    width:roomAdjustWidget.width*0.6
                    onValueChanged: {

                        light=adjustLight.value

                        if(light == adjustLight.minimum)lamp.light = false
                        else lamp.light = true


                        lamp.opacity = (light-adjustLight.minimum)/(adjustLight.maximum-adjustLight.minimum)
                    }
                }
            }
            Row {
                spacing: colSpacing

                Item{
                    height:roomAdjustWidget.height*0.20
                    width: height

                    Image {
                        anchors.fill:parent
                        source:"images/speaker_100x100.png"
                        smooth:true

                    }
                    Item{
                        anchors.fill:parent
                        id:speaker
                        CLHistogram{
                            id: histogram;
                            style: screen.style;
                            gradientWhenDefault:Gradient {
                                GradientStop { position: 0.0; color: "#8aff00" }
                                GradientStop { position: 1.0; color: "#57b229" }
                            }
                            colorWhenDefault:"#8aff00"
                            maxValue: 25;
                            anchors.right : parent.right;
                            anchors.bottom: parent.bottom;
                            width: parent.width
                            height: parent.height*1.3
                            opacity: 1;
                            y:-20
                        }

                    }
                    Image{
                        id:mute
                        anchors.fill:parent

                        source:"images/speaker_mute_100x100.png"
                        smooth:true

                    }

                    MouseArea {
                        id: mouseArea3

                        anchors.fill: parent
                        onClicked: {
                            if (adjustVolume.value != adjustVolume.minimum) {
                                screen.mute = !screen.mute
                                mute.opacity = (screen.mute)?1:0;
                                speaker.opacity = (screen.mute)?0:1;
                            }

                        }

                    }
                }
                CLSlider{
                    id:adjustVolume
                    style:screen.style
                    minimum:0
                    maximum:100
                    borderWidth: 3
                    height:roomAdjustWidget.height*0.15
                    width:roomAdjustWidget.width*0.6
                    onValueChanged: {
                        volume=adjustVolume.value
                        if( !onStartUp ) {
                            if(volume == adjustVolume.minimum) {
                                screen.mute = true
                                mute.opacity = 1
                                speaker.opacity = 0
                            } else {
                                screen.mute = false
                                mute.opacity = 0
                                histogramValues(value)
                                speaker.opacity = 1
                            }
                        }
                        onStartUp = false;
                    }
                }
            }
        }
    }
}
